package cs342.uwosh.edu.QuizApp;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

import cs342.uwosh.edu.R;
import cs342.uwosh.edu.QuizApp.database.Connect;
import cs342.uwosh.edu.QuizApp.thread.AsyncMethod;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;


public class LogInActivity extends Activity {
	
	 EditText unBox;
	 EditText pwBox;
	
	 /**
	  * Initializes references for user name and 
	  * password text boxes for later use
	  */
	 
	 public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login);
        unBox = ((EditText)findViewById(R.id.usernameTextBox));
        pwBox = ((EditText)findViewById(R.id.passwordTextBox));
	 }
	 
	 /**
	  * Attempts to log the user in
	  */
	 
	 public void okButton(View v) {
		 String username = unBox.getText().toString();
		 String password = pwBox.getText().toString();
		 if(isValidLogInData(username,password)) {
			Intent i = new Intent(this, QuizListActivity.class);
			i.putExtra("user", username);
			startActivity(i);
			finish();
		 } else {
			 loginErrorPrompt();
		 }
	 }
	 
	 /**
	  * Quits QuizApp
	  */
	 
	 public void cancelButton(View v) {
		 onBackPressed();
	 }
	 
	 /**
	  * Checks the users login data
	  */
	 
	 public boolean isValidLogInData(String username, String password) {
		 boolean isValid = true;
		 String authURL = "http://cs346.cs.uwosh.edu/students/gustrd63/Project3/auth.php?username=" + username + "&password=" + password;
		 Connect authenticate = new Connect();
		 AsyncMethod<Connect> am = new AsyncMethod<Connect>(Connect.class, authenticate, "authentication");
		 am.execute(authURL);
		 isValid = (Boolean)am.getResult();
		 return isValid;
	 }
	 
     public boolean testConnect(String authURL){
    	 boolean isValid = false;
    	 URL url;
         HttpURLConnection connection = null;  
         try{
             url = new URL(authURL);
             connection = (HttpURLConnection)url.openConnection();
             BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
             String line;
             StringBuffer response = new StringBuffer(); 
             while((line = in.readLine()) != null) {
                 response.append(line);
             }
             in.close();
             int value = Integer.parseInt(response.toString());
             if(value == 1){
            	 isValid = true;
             }
         }
         catch(Exception e){   
               e.printStackTrace();
         } 
         finally{   
             if(connection != null) 
                 connection.disconnect();
         }
    	 return isValid;
     }
	 /**
	  * Alerts the user that their log in data is wrong
	  */
	 
	 public void loginErrorPrompt() {
		AlertDialog.Builder alert = new AlertDialog.Builder(this);
		alert.setTitle("Log In Mismatch");
		alert.setMessage("Username and/or password do not match");
		alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int whichButton) {
				dialog.cancel();
			}
		});
		alert.show();
	 }
	 
	
}
